﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CFZS.WebHelper;
using System.Reflection;
using System.Configuration;

namespace CFZS.DALFactory
{
    /// <summary>
    /// 抽象工厂模式创建DAL。
    /// web.config 需要加入配置：(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口)  
    /// DataCache类在导出代码的文件夹里
    /// <appSettings>  
    /// <add key="DAL" value="CFZS.SQLServerDAL" /> (这里的命名空间根据实际情况更改为自己项目的命名空间)
    /// </appSettings> 
    /// </summary>
    public sealed class DataAccess
    {
        private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];

        /// <summary>
        /// 创建对象或从缓存获取
        /// </summary>
        public static object CreateObject(string AssemblyPath, string ClassNamespace)
        {
            object objType = DataCache.GetCache(ClassNamespace);//从缓存读取
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
                    DataCache.SetCache(ClassNamespace, objType);// 写入缓存
                }
                catch
                { }
            }
            return objType;
        }

        /// <summary>
        /// 创建数据层接口
        /// </summary>
        //public static t Create(string ClassName)
        //{
        //string ClassNamespace = AssemblyPath +"."+ ClassName;
        //object objType = CreateObject(AssemblyPath, ClassNamespace);
        //return (t)objType;
        //}
        /// <summary>
        /// 创建BaseOrg数据层接口。
        /// </summary>
        public static CFZS.IDAL.IBaseOrg CreateBaseOrg()
        {

            string ClassNamespace = AssemblyPath + ".BaseOrg";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (CFZS.IDAL.IBaseOrg)objType;
        }

        /// <summary>
        /// 创建BasePerson数据层接口。
        /// </summary>
        public static CFZS.IDAL.IBasePerson CreateBasePerson()
        {

            string ClassNamespace = AssemblyPath + ".BasePerson";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (CFZS.IDAL.IBasePerson)objType;
        }

        /// <summary>
        /// 创建BaseMenu数据层接口。
        /// </summary>
        public static CFZS.IDAL.IBaseMenu CreateBaseMenu()
        {

            string ClassNamespace = AssemblyPath + ".BaseMenu";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (CFZS.IDAL.IBaseMenu)objType;
        }

        /// <summary>
        /// 创建BasePower数据层接口。
        /// </summary>
        public static CFZS.IDAL.IBasePower CreateBasePower()
        {

            string ClassNamespace = AssemblyPath + ".BasePower";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (CFZS.IDAL.IBasePower)objType;
        }

        /// <summary>
        /// 创建BaseRole数据层接口。
        /// </summary>
        public static CFZS.IDAL.IBaseRole CreateBaseRole()
        {

            string ClassNamespace = AssemblyPath + ".BaseRole";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (CFZS.IDAL.IBaseRole)objType;
        }

        /// <summary>
        /// 创建BaseRolePower数据层接口。
        /// </summary>
        public static CFZS.IDAL.IBaseRolePower CreateBaseRolePower()
        {

            string ClassNamespace = AssemblyPath + ".BaseRolePower";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (CFZS.IDAL.IBaseRolePower)objType;
        }


        /// <summary>
        /// 创建baseUser数据层接口。
        /// </summary>
        public static CFZS.IDAL.IbaseUser CreatebaseUser()
        {

            string ClassNamespace = AssemblyPath + ".baseUser";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (CFZS.IDAL.IbaseUser)objType;
        }

        /// <summary>
        /// 创建baseUserPower数据层接口。
        /// </summary>
        public static CFZS.IDAL.IbaseUserPower CreatebaseUserPower()
        {

            string ClassNamespace = AssemblyPath + ".baseUserPower";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (CFZS.IDAL.IbaseUserPower)objType;
        }

        /// <summary>
        /// 创建BaseDictionary数据层接口。
        /// </summary>
        public static CFZS.IDAL.IBaseDictionary CreateBaseDictionary()
        {

            string ClassNamespace = AssemblyPath + ".BaseDictionary";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (CFZS.IDAL.IBaseDictionary)objType;
        }


        /// <summary>
        /// 创建CommAttachment数据层接口。
        /// </summary>
        public static CFZS.IDAL.ICommAttachment CreateCommAttachment()
        {

            string ClassNamespace = AssemblyPath + ".CommAttachment";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (CFZS.IDAL.ICommAttachment)objType;
        }

        /// <summary>
        /// 创建CommNews数据层接口。
        /// </summary>
        public static CFZS.IDAL.ICommNews CreateCommNews()
        {

            string ClassNamespace = AssemblyPath + ".CommNews";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (CFZS.IDAL.ICommNews)objType;
        }

        /// <summary>
        /// 创建CommNotepad数据层接口。
        /// </summary>
        public static CFZS.IDAL.ICommNotepad CreateCommNotepad()
        {

            string ClassNamespace = AssemblyPath + ".CommNotepad";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (CFZS.IDAL.ICommNotepad)objType;
        }
    }
}
